//
//  TypesetBtnList.m
//  GaiMeiYouKe
//
//  Created by ypwl on 15/8/7.
//  Copyright (c) 2015年 YPWL. All rights reserved.
//

#import "TypesetBtnList.h"
#import <QuartzCore/QuartzCore.h>
@implementation TypesetBtnList{
    
    UIButton*_lastButton;
}

#define BORDER_COLOR [UIColor blackColor]
#define BORDER_WIDTH 1.0f
#define CORNER_RADIUS 0.0f //圆角
#define LABEL_MARGIN 5.0f  //btn的横向间距
#define BOTTOM_MARGIN 5.0f  //btn的纵向间距
#define FONT_SIZE 14.0f
#define HORIZONTAL_PADDING 10.0f //btn 字体的左右距离
#define VERTICAL_PADDING 6.0f  //btn 字体的上下距离
#define BACKGROUND_COLOR [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00]
#define TEXT_COLOR [UIColor darkGrayColor]
#define MIN_WIDTH (self.frame.size.width-(4*5))/3.0


@synthesize view, textArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:view];
        self.titleColor=TEXT_COLOR;
        self.borderColor=BORDER_COLOR;
    }
    return self;
}


- (void)setTags:(NSArray *)array
{
    textArray = nil;
    textArray = [[NSArray alloc] initWithArray:array];
    sizeFit = CGSizeZero;
    [self display];
}

- (void)setLabelBackgroundColor:(UIColor *)color
{
    lblBackgroundColor = color;
    [self display];
}

- (void)setDefaultChoice:(NSString *)defaultChoice{
    
    _defaultChoice = defaultChoice;
    [self display];
}

- (void)display
{
    for (UILabel *subview in [self subviews]) {
        
        [subview removeFromSuperview];
    }
    float totalHeight = 0;
    
    CGRect previousFrame = CGRectZero;
    
    BOOL gotPreviousFrame = NO;
    
    for (int i=0; i<textArray.count; i++) {
        NSString*text=textArray[i];
        CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:CGSizeMake(self.frame.size.width, 1500) lineBreakMode:UILineBreakModeWordWrap];
        
        
        textSize.width += HORIZONTAL_PADDING*2;
        
        if (textSize.width < MIN_WIDTH) {
            textSize.width = MIN_WIDTH;
        }
        
        textSize.height += VERTICAL_PADDING*2;
        UIButton *button = nil;
    
        if (!gotPreviousFrame) {
//            button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textSize.width, textSize.height)];
            button = [UIButton buttonWithType:UIButtonTypeSystem];
            
            button.frame = CGRectMake(0, 0, textSize.width, textSize.height);
            
            [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
            
            totalHeight = textSize.height;
        } else {
            
            CGRect newRect = CGRectZero;
            
            if (previousFrame.origin.x + previousFrame.size.width + textSize.width + LABEL_MARGIN > self.frame.size.width) {
                
                newRect.origin = CGPointMake(0, previousFrame.origin.y + textSize.height + BOTTOM_MARGIN);
                
                totalHeight += textSize.height + BOTTOM_MARGIN;
                
            } else {
                newRect.origin = CGPointMake(previousFrame.origin.x + previousFrame.size.width + LABEL_MARGIN, previousFrame.origin.y);
            }
            
            newRect.size = textSize;

            button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = newRect;
            [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        previousFrame = button.frame;
        gotPreviousFrame = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
        
        button.tag=20+i;
        if (!lblBackgroundColor) {
            [button setBackgroundColor:BACKGROUND_COLOR];
        } else {
            [button setBackgroundColor:lblBackgroundColor];
        }
        
        
        
        [button setTitleColor:self.titleColor forState:UIControlStateNormal];
        [button setTitle:text forState:UIControlStateNormal];
        [button.layer setCornerRadius:CORNER_RADIUS];
        [button.layer setBorderColor:self.borderColor.CGColor];
        [button.layer setBorderWidth: BORDER_WIDTH];
        [button.layer setMasksToBounds:YES];
        
        
        
        if ([text isEqualToString:self.defaultChoice]) {
            
            [button setBackgroundImage:[UIImage imageNamed:@"自动投标-添加投标"] forState:0];
            
            [button setTitleColor:kColor_AppNavColor forState:UIControlStateNormal];
            
            button.layer.borderColor=[UIColor clearColor].CGColor;
            
            _lastButton=button;
            
            
        }

        
        [self addSubview:button];
    }
    sizeFit = CGSizeMake(self.frame.size.width, totalHeight + 1.0f);
    
}
-(void)setBoedercolorWith:(UIColor*)color{
    
    self.borderColor=color;
    
}
- (void)fittedSize
{
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, sizeFit.width, sizeFit.height);
}

-(void)clickAction:(UIButton *)btn{
    
    if (_lastButton) {
        
        [_lastButton setTitleColor:self.titleColor forState:UIControlStateNormal];
        
        [_lastButton setBackgroundImage:nil forState:0];

        
        _lastButton.layer.borderColor=UIColorFromString(@"#bbbbbb").CGColor;
        
        
            }
    
    
    [btn setBackgroundImage:[UIImage imageNamed:@"自动投标-添加投标"] forState:0];
    
    [btn setTitleColor:kColor_AppNavColor forState:UIControlStateNormal];
    
     btn.layer.borderColor=[UIColor clearColor].CGColor;
    _lastButton=btn;
    

    
    if (self.delegate && [_delegate respondsToSelector:@selector(clickWithTitle:Btn:typeListTitle:btnListView:)]) {
        [self.delegate clickWithTitle:btn.titleLabel.text Btn:btn typeListTitle:self.listTitle btnListView:self];
        
           }
    if (self.delegate && [_delegate respondsToSelector:@selector(clickWithBtn:)]) {
        [self.delegate clickWithBtn:btn];
    }
    
}

@end
